package com.common.utils.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.io.FileOutputStream;

/**
 * @Author: zengliang
 * @Description: 生成Excel文档通用类
 * @Date:Created in 11:29 2022/4/11
 * @Modified By:
 */
public class GenerateExcel {

    //默认宽度系数
    private int widthFactor = 256;

    public int getWidthFactor() {
        return widthFactor;
    }

    public void setWidthFactor(int widthFactor) {
        this.widthFactor = widthFactor;
    }

    public SXSSFWorkbook createWorkbook(){
        return new SXSSFWorkbook();
    }

    /**
     * 创建默认字体
     * @param sw
     * @return
     */
    public Font createFont(SXSSFWorkbook sw){
        Font font = sw.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 16);
        font.setFontHeight((short)11);
        font.setFontName("黑体");
        return font;
    }

    /**
     * 创建表格风格
     * @param sw
     * @return
     */
    public CellStyle createCellStyle(SXSSFWorkbook sw){
        CellStyle cellStyle = sw.createCellStyle();
        Font font = sw.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setFontName("黑体");
        font.setBold(true);
        cellStyle.setFont(font);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        return cellStyle;
    }

    /**
     * 获取Excel Cell
     * @param row
     * @param num
     * @return
     */
    public XSSFCell getCell(XSSFRow row, int num){
        XSSFCell cell;
        if (row.getCell(num) == null){
            cell = row.createCell(num);
        }else{
            cell = row.getCell(num);
        }
        return cell;
    }

    /**
     * 获取Excel Row
     * @param sheet
     * @param num
     * @return
     */
    public XSSFRow getXSSFRow(XSSFSheet sheet, int num){
        XSSFRow row;
        if (sheet.getRow(num) == null){
            row = sheet.createRow(num);
        }else{
            row = sheet.getRow(num);
        }
        return row;
    }

    /**
     * 生成Excel文件
     * @param filePath
     * @param fileName
     */
    public void createExcel(SXSSFWorkbook sw,String filePath,String fileName){
        try {
            FileOutputStream out = new FileOutputStream(filePath + fileName);
            sw.write(out);
            out.close();
            sw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 自动根据列宽度设置列宽
     * @param sheet
     * @param size
     */
    public void getAutoSizeColumn(Sheet sheet,int size) {
        for (int columnNum = 0; columnNum < size; columnNum++) {
            String sheetName = sheet.getSheetName();
            System.out.println(sheetName);
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
                Row currentRow = null;
                //当前行未被使用过
                if (sheet.getRow(rowNum) == null) {
                    //currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                }
                if(currentRow != null){
                    if (currentRow.getCell(columnNum) != null) {
                        Cell currentCell = currentRow.getCell(columnNum);
                        if (currentCell.getCellType() == CellType.STRING) {
                            int length = currentCell.getStringCellValue().getBytes().length;
                            if (columnWidth < length) {
                                columnWidth = length;
                            }
                        }
                    }
                }
            }
            sheet.setColumnWidth(columnNum, columnWidth * 256);
        }
    }
}
